﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Jidong
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    public class 资产编码查询 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            context.Response.CacheControl = "no-cache";
            context.Response.Expires = -1;

            string s = context.Request["s"];
            if (s == null) return;
            int id,fid;
            byte lv;
            string mc;
            if (!int.TryParse(s, out fid))
            {
                return;
            }

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = "select id,名称,LV,规格 from 资产编码 where FID="+fid;
            Cmd.CommandType = CommandType.Text;
            SqlDataReader Reader = Cmd.ExecuteReader();

            while (Reader.Read())
            {
                id = (int)Reader["ID"];
                lv = (byte)Reader["LV"];
                mc=(string)Reader["名称"];
                if (!Convert.IsDBNull(Reader["规格"]))
                {
                    mc += "(" + (string)Reader["规格"] + ")";
                }
                if(lv==0)
                {
                    context.Response.Write(id.ToString("D8") + "|1|" + (id/1000000).ToString("D2") + mc + "|/Icons/16x16/26.png\n");
                }
                else if (lv == 1)
                {
                    context.Response.Write(id.ToString("D8") + "|1|" + ((id/ 10000)%100).ToString("D2") + mc + "|/Icons/16x16/26.png\n");
                }
                else if (lv==2)
                {
                    context.Response.Write(id.ToString("D8") + "|1|" + ((id / 100) % 100).ToString("D2") + mc + "|/Icons/16x16/26.png\n");
                }
                else
                {
                    context.Response.Write(id.ToString("D8") + "|0|" + (id % 100).ToString("D2") + mc + "|/Icons/16x16/27.png\n");
                }

            }
            Reader.Close();
            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
